var table;
$(function() {
	table = dataTable.init('table', {
		url : RS.webPath + '/admin/rsUserSystem/query',
		toolbar : '#toolbar',
		cols : [ [ {
			checkbox : true,
			fixed : true
		}, {
			field : 'lockTime',
			title : '锁定时长(毫秒)',
			fixed : 'left'
		}, {
			field : 'deniedNum',
			title : '限制访问次数',
			fixed : 'left',
		}, {
			field : 'autoLifting',
			title : '是否自动解除',
			fixed : 'left',
			templet : function(val) {
				return auto(val.autoLifting);
			}
		}, {
			field : 'systemStatus',
			title : '状态',
			fixed : 'left',
			templet : function(val) {
				return status(val.systemStatus);
			}

		} ] ]
	});

});
function query() {
	table.search({
		userId : $('#userId').val()
	});
}
function add() {
	window.location.href = RS.webPath
			+ '/admin/rsUserSystem/edit?method=INSERT';
}
function del() {
	parent.layer.confirm("是否要删除选中的项目？", {
		icon : 3,
		title : '提示',
	}, function(index) {
		var selections = table.getAllSelected();
		if (selections.length < 1) {
			parent.layer.alert("您未选择任何一条记录");
			return;
		}
		var ids = [];
		for (var i = 0; i < selections.length; i++) {
			ids[i] = selections[i].systemId
		}
		$.ajax({
			url : RS.webPath + '/admin/rsUserSystem/del',
			type : 'post',
			dataType : 'json',
			data : '__paramSet__=' + JSON.stringify(ids),
			success : function(ret) {
				if (ret.status == 0) {
					parent.layer.alert('删除失败');
				} else {
					parent.layer.alert('删除成功');
				}
				$table.bootstrapTable("refresh");
			},
			error : function() {
				parent.layer.alert('删除失败');
				query();
			}
		});
	});
}
function edit() {
	var selections = table.getAllSelected();
	if (selections.length != 1) {
		parent.layer.alert("只能选择一个项目进行编辑");
		return;
	}
	if (selections[0].systemStatus == "1") {
		parent.layer.alert("只能编辑未发布信息");
		return;
	}
	console.log(selections[0].systemStatus)
	var id = selections[0].systemId;
	window.location.href = RS.webPath
			+ '/admin/rsUserSystem/edit?method=UPDATE&id=' + id;
}

function auto(value, rows, index) {
	if (value == "1") {
		return "自动解除";
	} else {
		return "不自动解除";
	}
}

function status(value, rows, index) {
	if (value == "1") {
		return "发布";
	} else {
		return "未发布";
	}
}

function issue() {
	parent.layer.confirm("是否要发布选中的项目？", {
		icon : 3,
		title : '提示',
	}, function(index) {
		var selections = table.getAllSelected();
		if (selections.length != 1) {
			parent.layer.alert("只能选择一个项目进行编辑");
			return;
		}
		var id = selections[0].systemId;
		$.ajax({
			url : RS.webPath + '/admin/rsUserSystem/issue',
			type : 'post',
			dataType : 'json',
			data : 'id=' + id,
			success : function(ret) {
				if (ret.status == 0) {
					parent.layer.alert('发布失败');
				} else {
					parent.layer.alert('发布成功');
				}
				query();
			}
		});
	});
}